package com.example.xinli.controller;

import com.example.xinli.domain.dto.ExUserDto;
import com.example.xinli.domain.dto.imgDTO;
import com.example.xinli.domain.vo.GetUserInfoVo;
import com.example.xinli.domain.vo.HuibidanganSubmitVo;
import com.example.xinli.domain.vo.ResponseBeanEnum;
import com.example.xinli.service.IUserService;
import com.example.xinli.domain.vo.ResponseBean;
import com.example.xinli.utils.TokenUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@Slf4j
@RequestMapping("/admin")
public class AdminController {


        @Autowired
        private RestTemplate restTemplate;


        @Autowired
        private IUserService userService;

        // 根据获取所有用户信息
        @RequestMapping("/setUser")
        @ResponseBody
        public ResponseBean setUserInfo(@RequestBody ExUserDto exUserDto) {
            return userService.setUserGroup(exUserDto);
        }

        @RequestMapping("/sendNotification")
        @ResponseBody
        public ResponseBean sendNotification(@RequestParam("id") String id) {

                String accessToken= userService.getAccessToken().getData().toString();

                log.info(accessToken);

                String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;

                // 构建消息体
                Map<String, Object> pushMsg = new HashMap<>();
                pushMsg.put("touser", id);
                pushMsg.put("template_id", "kAcfm-7a4wnQ03jYBqa_rplhsYjfJXNN71MhlMGADPg");

                Map<String, Object> data = new HashMap<>();
                Map<String, String> thing1 = new HashMap<>();
                thing1.put("value", "下一次干预已解锁");

                Map<String, String> thing3 = new HashMap<>();
                thing3.put("value", "快来完成吧");

                data.put("thing1", thing1);
                data.put("thing3", thing3);

                pushMsg.put("data", data);

                // 构建 HTTP 请求头
                HttpHeaders headers = new HttpHeaders();
                headers.set("Content-Type", "application/json");
                HttpEntity<Map<String, Object>> entity = new HttpEntity<>(pushMsg, headers);

                try {
                        // 发送请求
                        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
                        log.info(response.getBody());

                        // 解析为 JsonObject
                        // 解析为 JsonObject
                        ObjectMapper objectMapper = new ObjectMapper();
                        String responseBody = response.getBody();

// 将 JSON 字符串解析为 JsonNode 对象
                        JsonNode jsonNode = objectMapper.readTree(responseBody);

// 提取字段值
                        int errcode = jsonNode.get("errcode").asInt();
                        String errmsg = jsonNode.get("errmsg").asText();



                        return ResponseBean.success(errcode);
                } catch (Exception e) {
                        return ResponseBean.error(ResponseBeanEnum.ERROR);
                }
        }
}
